const CaptchaMini = require('../lib/captcha/captcha-mini')

require('../less/register.less')
document.ready(function () {
    let vcodeVal = ''
    let captchaCode = new CaptchaMini({
        fontSize: 50
    })
    captchaCode.draw(document.querySelector('#captcha1'), function (res) {
        vcodeVal = res
    })
    //获取dom
    let btn = document.getElementById('btn');
    let telnum = document.getElementById('telnum')
    let vf = document.getElementById('vf') //验证码
    let psd = document.getElementById('psd'); //密码
    let cfpsd = document.getElementById('cfpsd'); //确认密码
    let tips = document.getElementById('tips') //获取提示文字
    let login = document.getElementById('login') //获取提示文字
    //注册事件
    btn.addEventListener('click', function () {
        let telnumVal = telnum.value;
        let vfVal = vf.value //验证码的值
        let psdVal = psd.value; //输入密码值
        let cfpsdVal = cfpsd.value //确认密码
        //判定手机号码
        if (telnumVal == '') {
            tips.innerHTML = '没输入手机号码';
            return
        } else if (!window.utils.phone(telnumVal)) {
            tips.innerHTML = '输入手机号格式不正确';
            return
        } else if (vfVal == '') {
            tips.innerHTML = '未输入验证码';

            return
        } else if (vfVal.toLowerCase() != vcodeVal.toLowerCase()) {
            tips.innerHTML = '验证码不正确';
            return
        } else if (psdVal == '') {
            tips.innerHTML = '未输入密码';
            return
        } else if (!window.utils.pwd(psdVal)) {
            tips.innerHTML = '输入密码不正确';
            return
        } else if (cfpsdVal == '') {
            tips.innerHTML = '未确认密码';
        } else if (cfpsdVal != psdVal) {
            tips.innerHTML = '输入密码不一致'
            return
        } else {
            tips.innerHTML = ''
            //实现post提交
            window.$http.post('/users/add', {
                account: telnumVal,
                password: psdVal
            }, function (res) {
                if (res.status == 0) {
                    alert('注册成功')
                    window.location.href='./login.html'
                }
                else{
                    alert('该账号已经存在')
                }
            })


        }




    })

//点击密码登录,页面跳转
login.addEventListener('click',function(){

    window.location.href='./login.html'
})

})